package com.glodon.paas.account.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.glodon.paas.account.api.bean.Communicate;
import com.glodon.paas.account.biz.UserBiz;
import com.glodon.paas.account.dao.domain.UserEntity;
import com.glodon.paas.account.service.CommunicateBookService;
import com.glodon.paas.account.web.util.SecurityUtils;
import com.glodon.paas.consts.StringConst;


@Controller
@RequestMapping("/communicateBook")
public class CommunicateBookController{
	public static final Logger LOGGER = LoggerFactory.getLogger(HomeController.class);
	
	@Autowired
	CommunicateBookService  communicateBookService;
	@Autowired
	private UserBiz userBiz;

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public void addCommunicate( 
			@RequestBody Communicate communicate,
			ModelMap model
			){
		String userId = SecurityUtils.getCurrentUser().getId();
		communicateBookService.addCommunicate(userId, communicate.getContactId(), communicate.getRemarkName(),true);		 
		model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
	}

	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	public void deleteCommunicate( 
			@RequestParam(value = "id", required = false ) String id,
			@RequestParam(value = "contactId", required = false ) String contactId,
			ModelMap model
			){
		if(id != null){ // 根据id删除
			communicateBookService.removeById(id);
			model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
		}else if(contactId!=null){// 根据联系人id删除
			String userId = SecurityUtils.getCurrentUser().getId();
			communicateBookService.remove(userId, contactId);
			model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
		}else{// 参数错误
			model.addAttribute(StringConst.RESULT, StringConst.FAIL);			
		}		
	}
	
	@RequestMapping(value = "/updateRemark", method = RequestMethod.POST)
	public void updateRemark( 
			@RequestParam(value = "id", required = false ) String id,
			@RequestParam(value = "remarkName", required = false ) String remarkName,
			@RequestParam(value = "remarkText", required = false ) String remarkText,
			ModelMap model
			){
		communicateBookService.updateRemark(id, remarkName, remarkText);
		model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);	
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public void listCommunicate( 
			@RequestParam(value = "id", required = false, defaultValue="-1")  int orderType,
			@RequestParam(value = "startIndex", required = false, defaultValue="0") int startIndex,
			@RequestParam(value = "count", required = false, defaultValue="20") int count,
			ModelMap model
			){
		String userId = SecurityUtils.getCurrentUser().getId();
		
		List<Communicate> list = communicateBookService.query(userId, orderType, startIndex, count);
		model.addAttribute(StringConst.ITEMS, list);
	}
	
	@RequestMapping(value = "/listContactUser", method = RequestMethod.GET)
	public void listContactUser(ModelMap model){
		String userId = SecurityUtils.getCurrentUser().getId();		
		List<Communicate> list = communicateBookService.query(userId, null, 0, 1000);
		 		
		if(list.size() == 0){
			model.addAttribute(StringConst.ITEMS, new ArrayList<UserEntity>());
			 
		}
		// 对查询结果构造索引结构map
		Map<String,Communicate> map = new HashMap<String,Communicate>();
		for(Communicate item : list){
			map.put(item.getContactId(), item);
		}
		
		// 批量获取联系人信息 参数：userIds
		String[] userIds = new String[list.size()];
		for(int index=0; index<list.size(); index++){
			userIds[index] = list.get(index).getContactId();
		}			
		List<UserEntity> dblist = userBiz.getUserByIds(userIds);
		List<Map<String,String>> userlist = new ArrayList<Map<String,String>>();
		for(UserEntity item : dblist){
			Map<String,String> userInfo = new HashMap<String,String>();
			userlist.add(userInfo);
			
			String tempProperty = null;			
			//displayName
			tempProperty = map.get(item.getId()).getRemarkName();
			if(StringUtils.isNotBlank(tempProperty)){
				userInfo.put("displayName", tempProperty);
			}else{
				userInfo.put("displayName", item.displayName());				
			}
			//accountName
			userInfo.put("accountName", item.accountName());
		}
		  
		model.addAttribute(StringConst.ITEMS, userlist);
	}
	
	
	@RequestMapping(value = "/get", method = RequestMethod.GET)
	public void queryById( 
			@RequestParam(value = "id", required = true ) String id, 
			ModelMap model
			){
		Communicate item = communicateBookService.queryById(id);
		model.addAttribute(StringConst.RESULT, item);
	}
	
	@RequestMapping(value = "/count", method = RequestMethod.GET)
	public void count(ModelMap model){
		String userId = SecurityUtils.getCurrentUser().getId();
		String count = communicateBookService.count(userId);
		model.addAttribute(StringConst.RESULT, count);
	}
}
